package com.fenger.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * @PackageName: com.fenger.servlet
 * @ClassName: LoginServlet
 * @Description:
 * @author: fang
 * @date: 2021/10/31 10:00
 */
@WebServlet(name = "login", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    public static final String TOKEN = "xx";
    public static final Set<String> usernameSet = new HashSet<>(Arrays.asList("zhangsan", "lisi"));

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        if (usernameSet.contains(username)) {
            Cookie cookie = new Cookie("token", TOKEN);
            cookie.setPath("*/web_servlet_demo_Web_exploded/*");
            cookie.setMaxAge(60);
            resp.addCookie(cookie);
            HttpSession session = req.getSession();
            session.setAttribute("dsadas","dsadsa");
            req.getRequestDispatcher("/user").forward(req, resp);
        }
        return;
    }
}
